package com.ext.replacedemo;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * 优先级：局部替换-->判断忽略行-->整行替换。
 * <p>contains匹配到key后，通过String.replace将key替换成value，Key要尽可能的精确，避免误替换。
 * <p>若替换后的行仍然包含原Key，那么key应更精确，或使用整行替换JavaXml_ReplaceLine，并添加完成标志。
 */
public class JavaXml_Replace {

    public ArrayList<String> mJXKeyList = new ArrayList<>();
    public ArrayList<String> mJXValueList = new ArrayList<>();

    private LinkedHashMap<String, String> mJXHashMap = new LinkedHashMap<>();

    public JavaXml_Replace() {
    }

    public JavaXml_Replace(boolean migrateTo34, boolean migrateToX) {/*局部替换，优先级高于整行替换*/
        /*强制竖屏*/
//        mJXHashMap.put("<activity", "<activity\n    android:launchMode=\"singleTop\"\n    android:screenOrientation =\"sensorPortrait\"\n");

        if (migrateToX) {
            mJXHashMap.put("String getClipboardChanged(", "String  getClipboardChanged(,,,");

            mJXHashMap.put("(ShareDialog shareDialog)", "()");
            mJXHashMap.put("(mShareDialog);", "();");
            mJXHashMap.put("CheckUpdateView,", "");
            mJXHashMap.put("class ClipboardChecker {", "classClipboardChecker{");

            mJXHashMap.put("        AccessReasonMultiListView, AccessReasonListView {",
                    "        AccessReasonMultiListView , AccessReasonListView {" +
                            "\n    private final int TYPE_SHOW_RULE = 60;");
            mJXHashMap.put(".updateVistorOtherInfoDetail(60);", ".updateVistorOtherInfoDetail(TYPE_SHOW_RULE);");


            mJXHashMap.put("startPrintV2(String printContent) {", "startPrintV2(String printContents) {/*");
            mJXHashMap.put("//        BtPrinterActivity.", "*///        BtPrinter Activity.");

            mJXHashMap.put("System.currentTimeMillis()+\"_pdf\", System.currentTimeMillis() + \"_pdf\");", "System.currentTimeMillis() + \"_pdf\");");

            loadAndroidX();
        }

        if (mJXHashMap.size() > 0) {
            Set<String> keySet = mJXHashMap.keySet();
            Collection<String> values = mJXHashMap.values();
            mJXKeyList.addAll(Arrays.asList(keySet.toArray(new String[]{})));
            mJXValueList.addAll(Arrays.asList(values.toArray(new String[]{})));
        }
    }

    private void loadAndroidX() {
        mJXHashMap.put("@NonNull @androidx.annotation.NonNull", "@NonNull");
        mJXHashMap.put("@Nullable @androidx.annotation.Nullable", "@Nullable");

        //<editor-fold desc="v4">
        mJXHashMap.put("android.support.v4.os.ParcelableCompat", "androidx.core.os.ParcelableCompat");
        mJXHashMap.put("android.support.v4.os.ParcelableCompatCreatorCallbacks", "androidx.core.os.ParcelableCompatCreatorCallbacks");

        mJXHashMap.put("android.support.v4.app.LoaderManager", "androidx.loader.app.LoaderManager");
        mJXHashMap.put("android.support.v4.app.ActivityCompat", "androidx.core.app.ActivityCompat");
        mJXHashMap.put("android.support.v4.app.AppOpsManagerCompat", "androidx.core.app.AppOpsManagerCompat");
        mJXHashMap.put("android.support.v4.app.NotificationCompat", "androidx.core.app.NotificationCompat");
        mJXHashMap.put("android.support.v4.app.NotificationManagerCompat", "androidx.core.app.NotificationManagerCompat");
        mJXHashMap.put("android.support.v4.app.Fragment", "androidx.fragment.app.Fragment");
        mJXHashMap.put("android.support.v4.app.FragmentPagerAdapter", "androidx.fragment.app.FragmentPagerAdapter");
        mJXHashMap.put("android.support.v4.app.FragmentStatePagerAdapter", "androidx.fragment.app.FragmentStatePagerAdapter");
        mJXHashMap.put("android.support.v4.app.DialogFragment", "androidx.fragment.app.DialogFragment");
        mJXHashMap.put("android.support.v4.app.ListFragment", "androidx.fragment.app.ListFragment");

        mJXHashMap.put("android.support.v4.text.TextUtilsCompat", "androidx.core.text.TextUtilsCompat");
        mJXHashMap.put("android.support.v4.util.SparseArrayCompat", "androidx.collection.SparseArrayCompat");
        mJXHashMap.put("android.support.v4.util.ArrayMap", "androidx.collection.ArrayMap");
        mJXHashMap.put("android.support.v4.util.Pools", "androidx.core.util.Pools");
        mJXHashMap.put("android.support.v4.util.Pair", "androidx.core.util.Pair");

        mJXHashMap.put("android.support.v4.graphics.drawable.DrawableCompat", "androidx.core.graphics.drawable.DrawableCompat");
        mJXHashMap.put("android.support.v4.graphics.ColorUtils", "androidx.core.graphics.ColorUtils");
        mJXHashMap.put("android.support.v4.graphics.drawable.RoundedBitmapDrawable", "androidx.core.graphics.drawable.RoundedBitmapDrawable");
        mJXHashMap.put("android.support.v4.content.FileProvider", "androidx.core.content.FileProvider");
        mJXHashMap.put("android.support.v4.content.CursorLoader", "androidx.loader.content.CursorLoader");
        mJXHashMap.put("android.support.v4.content.Loader", "androidx.loader.content.Loader");
        mJXHashMap.put("android.support.v4.content.PermissionChecker", "androidx.core.content.PermissionChecker");
        mJXHashMap.put("android.support.v4.content.ContextCompat", "androidx.core.content.ContextCompat");
        mJXHashMap.put("android.support.v4.content.LocalBroadcastManager", "androidx.localbroadcastmanager.content.LocalBroadcastManager");
        mJXHashMap.put("android.support.v4.widget.EdgeEffectCompat", "androidx.core.widget.EdgeEffectCompat");
        mJXHashMap.put("android.support.v4.widget.ScrollerCompat", "androidx.core.widget.ScrollerCompat");
        mJXHashMap.put("android.support.v4.widget.TextViewCompat", "androidx.core.widget.TextViewCompat");
        mJXHashMap.put("android.support.v4.widget.NestedScrollView", "androidx.core.widget.NestedScrollView");
        mJXHashMap.put("android.support.v4.widget.SwipeRefreshLayout", "androidx.swiperefreshlayout.widget.SwipeRefreshLayout");
        mJXHashMap.put("android.support.v4.widget.DrawerLayout", "androidx.drawerlayout.widget.DrawerLayout");
        mJXHashMap.put("android.support.v4.widget.Space", "androidx.legacy.widget.Space");

        mJXHashMap.put("android.support.v4.view.AccessibilityDelegateCompat", "androidx.core.view.AccessibilityDelegateCompat");
        mJXHashMap.put("android.support.v4.view.accessibility.AccessibilityEventCompat", "androidx.core.view.accessibility.AccessibilityEventCompat");
        mJXHashMap.put("android.support.v4.view.accessibility.AccessibilityNodeInfoCompat", "androidx.core.view.accessibility.AccessibilityNodeInfoCompat");
        mJXHashMap.put("android.support.v4.view.accessibility.AccessibilityRecordCompat", "androidx.core.view.accessibility.AccessibilityRecordCompat");

        mJXHashMap.put("android.support.v4.view.GestureDetectorCompat", "androidx.core.view.GestureDetectorCompat");
        mJXHashMap.put("android.support.v4.view.GravityCompat", "androidx.core.view.GravityCompat");
        mJXHashMap.put("android.support.v4.view.NestedScrollingParent", "androidx.core.view.NestedScrollingParent");
        mJXHashMap.put("android.support.v4.view.ViewPager", "androidx.viewpager.widget.ViewPager");
        mJXHashMap.put("android.support.v4.view.ViewCompat", "androidx.core.view.ViewCompat");
        mJXHashMap.put("android.support.v4.view.ViewConfigurationCompat", "androidx.core.view.ViewConfigurationCompat");
        mJXHashMap.put("android.support.v4.view.MotionEventCompat", "androidx.core.view.MotionEventCompat");
        mJXHashMap.put("android.support.v4.view.VelocityTrackerCompat", "androidx.core.view.VelocityTrackerCompat");
        mJXHashMap.put("android.support.v4.view.PagerAdapter", "androidx.viewpager.widget.PagerAdapter");

        mJXHashMap.put("android.support.v4.view.animation.FastOutLinearInInterpolator", "androidx.interpolator.view.animation.FastOutLinearInInterpolator");
        mJXHashMap.put("android.support.v4.view.animation.FastOutSlowInInterpolator", "androidx.interpolator.view.animation.FastOutSlowInInterpolator");
        mJXHashMap.put("android.support.v4.view.animation.LinearOutSlowInInterpolator", "androidx.interpolator.view.animation.LinearOutSlowInInterpolator");
        // </editor-fold>

        //<editor-fold desc="v7">
        mJXHashMap.put("android.support.v7.widget.AppCompatImageView", "androidx.appcompat.widget.AppCompatImageView");
        mJXHashMap.put("android.support.v7.widget.AppCompatTextView", "androidx.appcompat.widget.AppCompatTextView");
        mJXHashMap.put("android.support.v7.widget.AppCompatCheckBox", "androidx.appcompat.widget.AppCompatCheckBox");
        mJXHashMap.put("android.support.v7.widget.AppCompatButton", "androidx.appcompat.widget.AppCompatButton");
        mJXHashMap.put("android.support.v7.widget.AppCompatSeekBar", "androidx.appcompat.widget.AppCompatSeekBar");
        mJXHashMap.put("android.support.v7.widget.AppCompatEditText", "androidx.appcompat.widget.AppCompatEditText");
        mJXHashMap.put("android.support.v7.widget.ActionBarContainer", "androidx.appcompat.widget.ActionBarContainer");
        mJXHashMap.put("android.support.v7.widget.LinearLayoutCompat", "androidx.appcompat.widget.LinearLayoutCompat");
        mJXHashMap.put("android.support.v7.widget.ListPopupWindow", "androidx.appcompat.widget.ListPopupWindow");
        mJXHashMap.put("android.support.v7.widget.ButtonBarLayout", "androidx.appcompat.widget.ButtonBarLayout");
        mJXHashMap.put("android.support.v7.widget.Toolbar", "androidx.appcompat.widget.Toolbar");

        mJXHashMap.put("android.support.v7.widget.RecyclerView", "androidx.recyclerview.widget.RecyclerView");
        mJXHashMap.put("android.support.v7.widget.LinearLayoutManager", "androidx.recyclerview.widget.LinearLayoutManager");
        mJXHashMap.put("android.support.v7.widget.StaggeredGridLayoutManager", "androidx.recyclerview.widget.StaggeredGridLayoutManager");
        mJXHashMap.put("android.support.v7.widget.OrientationHelper", "androidx.recyclerview.widget.OrientationHelper");
        mJXHashMap.put("android.support.v7.widget.DefaultItemAnimator", "androidx.recyclerview.widget.DefaultItemAnimator");
        mJXHashMap.put("android.support.v7.widget.GridLayoutManager", "androidx.recyclerview.widget.GridLayoutManager");
        mJXHashMap.put("android.support.v7.widget.SimpleItemAnimator", "androidx.recyclerview.widget.SimpleItemAnimator");
        mJXHashMap.put("android.support.v7.widget.PagerSnapHelper", "androidx.recyclerview.widget.PagerSnapHelper");
        mJXHashMap.put("android.support.v7.widget.DividerItemDecoration", "androidx.recyclerview.widget.DividerItemDecoration");
        mJXHashMap.put("android.support.v7.widget.helper.ItemTouchHelper", "androidx.recyclerview.widget.ItemTouchHelper");

        mJXHashMap.put("android.support.v7.widget.CardView", "androidx.cardview.widget.CardView");
        mJXHashMap.put("android.support.v7.widget.RoundRectDrawable", "androidx.cardview.widget.RoundRectDrawable");

        mJXHashMap.put("android.support.v7.view.ContextThemeWrapper", "androidx.appcompat.view.ContextThemeWrapper");
        mJXHashMap.put("android.support.v7.app.AlertDialog", "androidx.appcompat.app.AlertDialog");
        mJXHashMap.put("android.support.v7.app.AppCompatDelegate", "androidx.appcompat.app.AppCompatDelegate");
        mJXHashMap.put("android.support.v7.app.AppCompatViewInflater", "androidx.appcompat.app.AppCompatViewInflater");
        mJXHashMap.put("android.support.v7.app.SkinAppCompatDelegateImpl", "androidx.appcompat.app.SkinAppCompatDelegateImpl");
        mJXHashMap.put("android.support.v7.app.ActionBar", "androidx.appcompat.app.ActionBar");
        mJXHashMap.put("android.support.v7.app.AppCompatActivity", "androidx.appcompat.app.AppCompatActivity");
        // </editor-fold>

        //<editor-fold desc="Other">
        mJXHashMap.put("android.support.annotation.ColorRes", "androidx.annotation.ColorRes");
        mJXHashMap.put("android.support.annotation.DimenRes", "androidx.annotation.DimenRes");
        mJXHashMap.put("android.support.annotation.DrawableRes", "androidx.annotation.DrawableRes");
        mJXHashMap.put("android.support.annotation.LayoutRes", "androidx.annotation.LayoutRes");
        mJXHashMap.put("android.support.annotation.IdRes", "androidx.annotation.IdRes");
        mJXHashMap.put("android.support.annotation.StringRes", "androidx.annotation.StringRes");
        mJXHashMap.put("android.support.annotation.StyleRes", "androidx.annotation.StyleRes");

        mJXHashMap.put("android.support.annotation.ColorInt", "androidx.annotation.ColorInt");
        mJXHashMap.put("android.support.annotation.IntDef", "androidx.annotation.IntDef");
        mJXHashMap.put("android.support.annotation.IntRange", "androidx.annotation.IntRange");
        mJXHashMap.put("android.support.annotation.FloatRange", "androidx.annotation.FloatRange");

        mJXHashMap.put("android.support.annotation.UiThread", "androidx.annotation.UiThread");
        mJXHashMap.put("android.support.annotation.MainThread", "androidx.annotation.MainThread");
        mJXHashMap.put("android.support.annotation.WorkerThread", "androidx.annotation.WorkerThread");
        mJXHashMap.put("android.support.annotation.AnyThread", "androidx.annotation.AnyThread");

        mJXHashMap.put("android.support.annotation.CheckResult", "androidx.annotation.CheckResult");
        mJXHashMap.put("android.support.annotation.CallSuper", "androidx.annotation.CallSuper");
        mJXHashMap.put("android.support.annotation.NonNull", "androidx.annotation.NonNull");
        mJXHashMap.put("android.support.annotation.Nullable", "androidx.annotation.Nullable");
        mJXHashMap.put("android.support.annotation.RequiresApi", "androidx.annotation.RequiresApi");
        mJXHashMap.put("android.support.annotation.VisibleForTesting", "androidx.annotation.VisibleForTesting");
//
        mJXHashMap.put("android.support.customtabs.CustomTabsClient", "androidx.browser.customtabs.CustomTabsClient");
        mJXHashMap.put("android.support.customtabs.CustomTabsCallback", "androidx.browser.customtabs.CustomTabsCallback");
        mJXHashMap.put("android.support.customtabs.CustomTabsService", "androidx.browser.customtabs.CustomTabsService");
        mJXHashMap.put("android.support.customtabs.CustomTabsSession", "androidx.browser.customtabs.CustomTabsSession");
        mJXHashMap.put("android.support.customtabs.CustomTabsIntent", "androidx.browser.customtabs.CustomTabsIntent");
//
        mJXHashMap.put("android.support.constraint.ConstraintLayout", "androidx.constraintlayout.widget.ConstraintLayout");
        mJXHashMap.put("android.support.constraint.ConstraintSet", "androidx.constraintlayout.widget.ConstraintSet");
        mJXHashMap.put("android.support.constraint.Guideline", "androidx.constraintlayout.widget.Guideline");
//

//
        mJXHashMap.put("android.support.design.widget.CoordinatorLayout", "androidx.coordinatorlayout.widget.CoordinatorLayout");
        mJXHashMap.put("android.support.design.widget.Snackbar", "com.google.android.material.snackbar.Snackbar");
        mJXHashMap.put("android.support.design.widget.TabLayout", "com.google.android.material.tabs.TabLayout");
        mJXHashMap.put("android.support.design.widget.TabItem", "com.google.android.material.tabs.TabItem");
        mJXHashMap.put("android.support.design.widget.AnimationUtils", "com.google.android.material.animation.AnimationUtils");
        mJXHashMap.put("android.support.design.widget.AnimatorSetCompat", "com.google.android.material.animation.AnimatorSetCompat");
        mJXHashMap.put("android.support.design.widget.AppBarLayout", "com.google.android.material.appbar.AppBarLayout");
        mJXHashMap.put("android.support.design.widget.CollapsingToolbarLayout", "com.google.android.material.appbar.CollapsingToolbarLayout");
        mJXHashMap.put("android.support.design.widget.FloatingActionButton", "com.google.android.material.floatingactionbutton.FloatingActionButton");
        mJXHashMap.put("android.support.design.widget.TextInputLayout", "com.google.android.material.textfield.TextInputLayout");
        mJXHashMap.put("android.support.design.widget.TextInputEditText", "com.google.android.material.textfield.TextInputEditText");
        mJXHashMap.put("android.support.design.widget.BottomNavigationView", "com.google.android.material.bottomnavigation.BottomNavigationView");
        mJXHashMap.put("android.support.design.widget.NavigationView", "com.google.android.material.navigation.NavigationView");
        mJXHashMap.put("android.support.design.widget.BottomSheetDialogFragment", "com.google.android.material.bottomsheet.BottomSheetDialogFragment");
//
        mJXHashMap.put("android.support.multidex.MultiDex", "androidx.multidex.MultiDex");
        mJXHashMap.put("android.support.multidex.MultiDexApplication", "androidx.multidex.MultiDexApplication");
//
        mJXHashMap.put("android.arch.lifecycle.MutableLiveData", "androidx.lifecycle.MutableLiveData");
        mJXHashMap.put("android.arch.lifecycle.LiveData", "androidx.lifecycle.LiveData");
        mJXHashMap.put("android.arch.lifecycle.Observer", "androidx.lifecycle.Observer");
        mJXHashMap.put("android.arch.lifecycle.ViewModelProviders", "androidx.lifecycle.ViewModelProviders");
        mJXHashMap.put("android.arch.lifecycle.AndroidViewModel", "androidx.lifecycle.AndroidViewModel");
        mJXHashMap.put("android.arch.lifecycle.LifecycleObserver", "androidx.lifecycle.LifecycleObserver");
        mJXHashMap.put("android.arch.core.util.Function", "androidx.arch.core.util.Function");
//
        mJXHashMap.put("android.databinding.DataBindingUtil", "androidx.databinding.DataBindingUtil");
        mJXHashMap.put("android.databinding.BaseObservable", "androidx.databinding.BaseObservable");
        mJXHashMap.put("android.databinding.Bindable", "androidx.databinding.Bindable");
        mJXHashMap.put("android.databinding.ViewDataBinding", "androidx.databinding.ViewDataBinding");
        mJXHashMap.put("android.databinding.BindingAdapter", "androidx.databinding.BindingAdapter");
        //</editor-fold>
    }

    @Test
    public void checkAndroidX() {
        int size = mJXKeyList.size();

        for (int i = 0; i < size; i++) {
            String keyStr = mJXKeyList.get(i);
            String[] kSplit = keyStr.split("\\.");
            String k = kSplit[kSplit.length - 1];

            String valueStr = mJXValueList.get(i);
            String[] vSplit = valueStr.split("\\.");
            String v = vSplit[vSplit.length - 1];

            if (!k.equalsIgnoreCase(v)) {
                System.out.println("不一致-->\"" + keyStr + "\", \"" + valueStr + "\"");
            }
        }
    }
}